package com.boc.langxiang.ui.adapter.me;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.boc.langxiang.App;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.me.ConcernsActivityContract;
import com.boc.langxiang.entity.response.ArticleEntity;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.util.StringUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jakewharton.rxbinding.view.RxView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by admin on 2016/6/27.
 */
public class ConcernsRVAdapter extends BaseQuickAdapter<ArticleEntity> {

    private long lastTime = 0l;
    private long currentTime;
    private ConcernsActivityContract.View mView;

    public ConcernsRVAdapter(List<ArticleEntity> data, ConcernsActivityContract.View view) {
        super(R.layout.item_me_concern, data);
        this.mView = view;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ArticleEntity item) {
        helper.setText(R.id.tv_me_concern_title, item.getTitle())
                .setText(R.id.tv_me_concern_subtitle, "来自你订阅的\t" + (StringUtil.isEmpty(item.getAuthor()) ? "" : item.getAuthor()))
                .setText(R.id.btn_me_concern, "1".equals(item.getType()) ? "已关注" : "取消关注")
                .setText(R.id.tv_me_concern_editnum, StringUtil.isEmpty(item.getClick()) ? "0" : item.getClick())
                .setOnClickListener(R.id.btn_me_concern, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        currentTime = SystemClock.uptimeMillis();
                        if (currentTime - lastTime > 2000l) {
                            View cancelView = View.inflate(mContext, R.layout.popwindow_me_concern_cancel, null);
                            TextView tvPerson = (TextView) cancelView.findViewById(R.id.tv_me_concern_person);
                            tvPerson.setText("不再关注" + (StringUtil.isEmpty(item.getAuthor()) ? "?" : item.getAuthor() + "?"));
                            final PopupWindow cancelPopupWindow = new PopupWindow(cancelView, (int) (App.getInstance().getResources().getDisplayMetrics().widthPixels), LinearLayout.LayoutParams.WRAP_CONTENT);
                            cancelPopupWindow.setAnimationStyle(R.style.me_pop_anim_bottom_style);
                            cancelPopupWindow.setFocusable(true);
                            cancelPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                            cancelPopupWindow.setOutsideTouchable(true);
                            mView.setBackgroundAlpht(0.5f);
                            mView.showPopupWindow(cancelView, cancelPopupWindow);
                            cancelPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                                @Override
                                public void onDismiss() {
                                    mView.setBackgroundAlpht(1f);
                                }
                            });
                            RxView.clicks(cancelPopupWindow.getContentView().findViewById(R.id.tv_me_concern_no)).throttleFirst(500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Action1<Void>() {
                                        @Override
                                        public void call(Void aVoid) {
                                            Log.d(TAG, "id------------" + item.getId());
                                            Log.d(TAG, "id------------" + getData().get(helper.getAdapterPosition()).getId());
                                            mView.onNotConcern(cancelPopupWindow, getData().get(helper.getAdapterPosition()).getId(),helper.getLayoutPosition());
                                            mView.setBackgroundAlpht(1f);
                                        }
                                    });

                            RxView.clicks(cancelPopupWindow.getContentView().findViewById(R.id.tv_me_concern_cancel)).throttleFirst(500, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Action1<Void>() {
                                        @Override
                                        public void call(Void aVoid) {
                                            mView.onCancel(cancelPopupWindow);
                                            mView.setBackgroundAlpht(1f);
                                        }
                                    });
                        }
                    }
                });

        PicassoImageLoader.getInstance().displayUrlImage(mContext, item.getHeadphotourl(), (ImageView) helper.getView(R.id.esv_me_concern_headimg));
        PicassoImageLoader.getInstance().displayUrlImage(mContext, item.getPhotourl(), (ImageView) helper.getView(R.id.iv_me_concerns_bg));
    }
}
